<template>
  <div class="co_party_list">
    <div class="search-box">
      <Row type="flex" justify="space-between">
        <Col class="search-item">
          <span class="search-label">合作方名称:</span>
          <Input
            suffix="ios-search"
            search
            v-model="searchObj.partnerName"
            @on-search="searchName"
            @on-keyup="filterSpace"
            :maxlength="30"
            placeholder="请输入"
          ></Input>
        </Col>
        <Col class="search-item" style="margin-right:30px;">
          <Button
            class="search-btn"
            type="primary"
            @click="doSearch"
            v-if="hasPermission('moms:globalsetting:partnercreate')"
          >添加</Button>
        </Col>
      </Row>
    </div>
    <Table
      :loading="loading"
      :height="this.tableHeight"
      ref="selection"
      :columns="columns"
      :data="partyList"
      stripe
      class="table-list"
    >
      <template slot-scope="{ row, index }" slot="action">
        <i
          class="iconsys icondetail"
          @click="look(row,index)"
          title="查看详情"
          v-if="hasPermission('moms:globalsetting:partnerdetail')"
        ></i>
        <i
          class="iconsys iconedit"
          @click="uptShow(row,index)"
          title="编辑"
          v-if="hasPermission('moms:globalsetting:partnerupdate')"
        ></i>
        <Poptip
          confirm
          title="你确定要删除吗？"
          @on-ok="ok(row, index)"
          @on-cancel="cancel"
          class="poptip_del"
        >
          <i
            class="iconsys icondel"
            title="删除"
            v-if="hasPermission('moms:globalsetting:partnerdelete')"
          ></i>
        </Poptip>
      </template>
    </Table>
    <div class="page-box">
      <Page
        :total="pageObj.total"
        :page-size="pageObj.pageSize"
        :current="pageObj.pageIndex"
        :page-size-opts="[10, 20, 50, 100]"
        show-total
        show-sizer
        @on-change="changePageIndex"
        @on-page-size-change="changePageSize"
      />
    </div>
  </div>
</template>
<script>
import { getePartneList, delPartnerInfo } from '@/api/system/fishing.js'
export default {
  name: 'cooperation_party_list',
  data () {
    return {
      searchObj: {
        partnerName: ''
      },
      loading: false,
      tableHeight: 0,
      pageObj: {
        searchName: '',
        pageIndex: 1,
        pageSize: 10,
        total: 0
      },
      columns: [
        {
          title: 'ID',
          key: 'partnerId',
          align: 'center'
        },
        {
          title: '合作方名称',
          key: 'partnerName',
          align: 'center'
        },
        {
          title: '最后操作时间',
          key: 'updatedAt',
          align: 'center'
        }
      ],
      partyList: []
    }
  },
  watch: {
    '$route' (newRoute) {
      if (newRoute.name === 'cooperation_party_list') {
        this.pageObj.pageIndex = 1
        this.getlist()
      }
    }
  },
  mounted () {
    this.getlist()
    if (this.hasPermission(['moms:globalsetting:partnerdetail', 'moms:globalsetting:partnerupdate', 'moms:globalsetting:partnerdelete'])) {
      this.columns.push({
        title: '操作',
        width: 300,
        slot: 'action'
      })
    }
  },
  methods: {
    getlist () {
      this.loading = true
      getePartneList({
        merchantId: this.$store.state.user.merchantId,
        partnerName: this.searchObj.partnerName,
        offset: (this.pageObj.pageIndex - 1) * this.pageObj.pageSize,
        partnerStatus: 0,
        limit: this.pageObj.pageSize
      }).then(res => {
        this.loading = false
        if (res.data.status === 200) {
          this.partyList = res.data.data
          this.pageObj.total = res.data.totalCount
        } else {
          this.$Message.error(`${res.data.error}`)
        }
      })
    },
    searchName () {
      this.pageObj.pageIndex = 1
      this.getlist()
    },
    doSearch () {
      this.$router.push({ name: 'cooperation_party_create' })
    },
    filterSpace () {
      this.searchObj.partnerName = this.searchObj.partnerName ? this.searchObj.partnerName.replace(/\s+/g, '') : ''
    },
    look (row, index) {
      this.$router.push({ name: 'cooperation_party_detail', query: { id: row.partnerId } })
    },
    uptShow (row, index) {
      this.$router.push({ name: 'cooperation_party_edit', query: { id: row.partnerId } })
    },
    ok (row) {
      delPartnerInfo({
        merchantId: this.$store.state.user.merchantId,
        partnerId: row.partnerId,
        updater: this.$store.state.user.userId,
        updaterName: this.$store.state.user.userName
      }).then(res => {
        if (res.data.status === 200) {
          this.$Message.success('删除成功')
          this.getlist()
          if (this.pageObj.total % this.pageObj.pageSize === 1) {
            if (this.pageObj.pageIndex > 1) {
              this.pageObj.pageIndex--
              this.getlist()
            }
          }
        } else {
          this.$Message.error(`${res.data.error}`)
        }
      })
    },
    cancel () { },
    changePageIndex (index) {
      this.pageObj.pageIndex = index
      this.getlist()
    },
    changePageSize (size) {
      this.pageObj.pageSize = size
      this.pageObj.pageIndex = 1
      this.getlist()
    }
  }
}
</script>
<style lang="less">
.co_party_list .ivu-poptip-confirm .ivu-poptip-body .ivu-icon {
  left: 10px;
}
</style>
